

/**
 *查询dtu列表与搜索框的模糊查询，有模糊查询，传dtu名称字段
 */
    var Grid = BUI.Grid,
        Store = BUI.Data.Store,
        columns = [
            {title: '序号', dataIndex: 'dtuId', renderer: function (value,obj,index) {
                    return index+1;
                }},
            {title : 'DTU名称',dataIndex :'code'},
            {title : 'DTU密码',dataIndex :'password'},
            {title : '通讯波特率',dataIndex :'baudRate'},
            {title : '备注',dataIndex :'remark',renderer: function (value) {
                    return value==""?"____":value;
                }
            },
            {title : '状态',dataIndex :'sts', renderer: function (value) {
                    return value==0?"<span style='color:#d9534f'>离线</span>":"<span>在线</span>";
                }
            },
            {title: '时间', dataIndex: 'createTime', renderer: function (value) {
                    return new Date(value).Format("yyyy-MM-dd hh:mm:ss");
                }
            },
            {title : '操作',dataIndex : 'dtuId', renderer : function (value) {
                    var option1 = "<a href='javascript:editDtu(" + value + ")'>编辑</a>";
                    var option2 = '<a href="javascript: delOneDtu(' + value + ');">删除</a>';
                    return option1 + "&nbsp;&nbsp;&nbsp;&nbsp;" + option2;
                }
            }
        ];

    //分页查询
    var store = new Store({
            url: 'rest/web/dtu/selectDtuPage',
            autoLoad: true, //自动加载数据
            params: { //配置初始请求的参数
                length: '15',
                start:'0'
            },
            pageSize: 15,	// 配置分页数目
            root: 'data.list',
            totalProperty: 'data.totalCount'
        }),
        grid = new Grid.Grid({
            render:'#grid',
            columns : columns,
            loadMask: true, //加载数据时显示屏蔽层
            store: store,
            plugins : [Grid.Plugins.CheckSelection], // 插件形式引入多选表格
            // 底部工具栏
            bbar:{
                pagingBar:true
            }
        });

    grid.render();
    //模糊搜索
    var form = new BUI.Form.HForm({
        srcNode: '#searchForm'
    }).render();

    form.on('beforesubmit', function (ev) {
        //序列化成对象
        var obj = form.serializeToObject();
        obj.start = 0; //返回第一页
        store.load(obj);
        return false;
    });

/**
 *添加dtu
 *
 */
var addForm = new BUI.Form.Form({
    srcNode: '#addForm',
    submitType: 'ajax',
    callback: function (data) {
        var obj = new Object();
        obj.start = 0; //返回第一页
        store.load(obj);
        addDialog.close();
    }
}).render();

var addDialog = new BUI.Overlay.Dialog({
    title: '新增Dtu',
    width: 500,
    height: 320,
    contentId: 'addContent',
    success: function () {
        if (addForm.isValid()) {
            addForm.submit();
        } else {
            addForm.valid();
        }
    }
});
function addDtu(){
    addForm.clearFields();
    addDialog.show();
}

/**
 *编辑tu
 * 表单提交方式BUI的editForm.submit()方式
 */
function editDtu(id) {
    $.ajax({
        type: "GET",
        dataType: "json",
        url: " /rest/web/dtu/selectDtuDetail",
        data: {dtuId: id},
        success: function (data) {
            var form = $("#editForm")[0];
            BUI.FormHelper.setFields(form, data.data);
            editDialog.show();

        }
    });
}
var editForm = new BUI.Form.Form({
    srcNode: '#editForm',
    submitType: 'ajax',
    callback: function (data) {
        var obj = new Object();
        obj.start = 0; //返回第一页
        store.load(obj);
        editDialog.close();
    }
});
editForm.render();

var editDialog = new BUI.Overlay.Dialog({
    title: '编辑DTU',
    width: 500,
    height: 320,
    contentId: 'editContent',
    success: function () {
        if (editForm.isValid()) {
            editForm.submit();
        } else {
            editForm.valid();
        }
    }
});

/**删除单个Dtu**/
function delOneDtu(value) {
    BUI.Message.Confirm('<h4 class="title-p">提示信息</h4><p>您确认要删除吗？</p>', function () {
        $.ajax({
            type: "POST",
            url: "/rest/web/dtu/deleteDtu",
            dataType: "json",
            data: {
                dtuId: value
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });
    }, 'question');
}
/**删除多个Dtu**/
//删除选中的记录--批量删除
function delBtn() {
    var selections = grid.getSelection();
    var ids = new Array();
    for (var i = 0; i < selections.length; i++) {
        ids[i] = selections[i].dtuId.toString()
    }
    BUI.Message.Confirm('<h4 class="title-p">提示信息</h4><p>您确认要删除吗？</p>', function () {
        $.ajax({
            type: "POST",
            url: "/rest/web/dtu/deleteDtus",
            dataType: "json",
            data: {
                ids: ids
            },
            success: function (data) {
                var obj = new Object();
                obj.start = 0; //返回第一页
                store.load(obj);
            }
        });
    }, 'question');
}